package cn.yiufung.algorithm.search.binary;

/**
 * @author 姚锋
 * @createTime 2024年07月04日 15:19:00
 */
public class BinarySearchTest {

    private static int[] largeArray = generateLargeArray(100000);

    public static void main(String[] args) {
        runLoopTests();
        runRecursionTests();
        runLoopBalanceTests();
    }

    /**
     * 测试循环方式二分查找
     */
    private static void runLoopTests() {
        System.out.println("Loop Binary Search Tests:");
        testLoopBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, 7, 3);
        testLoopBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, 8, -1);
        testLoopBinarySearch(new int[]{}, 1, -1);
        testLoopBinarySearch(null, 1, -1);
        testLoopBinarySearch(new int[]{5}, 5, 0);
        testLoopBinarySearch(new int[]{5}, 6, -1);
        testLoopBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, -3, -1);
        testLoopBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, 20, -1);
        testLoopBinarySearch(largeArray, 50000, 50000);
    }

    /**
     * 测试递归方式二分查找
     */
    private static void runRecursionTests() {
        System.out.println("Recursion Binary Search Tests:");
        testRecursionBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, 7, 3);
        testRecursionBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, 8, -1);
        testRecursionBinarySearch(new int[]{}, 1, -1);
        testRecursionBinarySearch(null, 1, -1);
        testRecursionBinarySearch(new int[]{5}, 5, 0);
        testRecursionBinarySearch(new int[]{5}, 6, -1);
        testRecursionBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, -3, -1);
        testRecursionBinarySearch(new int[]{1, 3, 5, 7, 9, 11}, 20, -1);
        testRecursionBinarySearch(largeArray, 50000, 50000);
    }

    /**
     * 测试循环平衡方式二分查找
     */
    private static void runLoopBalanceTests() {
        System.out.println("Loop Binary Search Balance Tests:");
        testLoopBinarySearchBalance(new int[]{1, 3, 5, 7, 9, 11}, 7, 3);
        testLoopBinarySearchBalance(new int[]{1, 3, 5, 7, 9, 11}, 8, -1);
        testLoopBinarySearchBalance(new int[]{}, 1, -1);
        testLoopBinarySearchBalance(null, 1, -1);
        testLoopBinarySearchBalance(new int[]{5}, 5, 0);
        testLoopBinarySearchBalance(new int[]{5}, 6, -1);
        testLoopBinarySearchBalance(new int[]{1, 3, 5, 7, 9, 11}, -3, -1);
        testLoopBinarySearchBalance(new int[]{1, 3, 5, 7, 9, 11}, 20, -1);
        testLoopBinarySearchBalance(largeArray, 50000, 50000);
    }

    private static void testLoopBinarySearch(int[] arr, int target, int expected) {
        int result = BinarySearch.loopBinarySearch(arr, target);
        System.out.println("期望值：" + expected + "，实际值：" + result);
    }

    private static void testRecursionBinarySearch(int[] arr, int target, int expected) {
        int result = arr == null ? BinarySearch.recursionBinarySearch(arr, target, 0, 0) :
                BinarySearch.recursionBinarySearch(arr, target, 0, arr.length - 1);
        System.out.println("期望值：" + expected + "，实际值：" + result);
    }

    private static void testLoopBinarySearchBalance(int[] arr, int target, int expected) {
        int result = BinarySearch.loopBinarySearchBalance(arr, target);
        System.out.println("期望值：" + expected + "，实际值：" + result);
    }

    private static int[] generateLargeArray(int size) {
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = i;
        }
        return arr;
    }

}
